package com.wdzg.member.generate.freemaker.generator.element;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.wdzg.member.generate.freemaker.generator.BaseGenerator;
import com.wdzg.member.generate.freemaker.generator.GeneratorHtml;
import com.wdzg.member.generate.freemaker.generator.bean.Column;
import com.wdzg.member.generate.freemaker.generator.bean.XmlBean;

public class ShowGenerator extends BaseGenerator {

	public ShowGenerator(XmlBean bean) {
		super(bean);
	}

	@Override
	public boolean generatorHtml(String ftlPath) {
		try {
			ftlFile = new File(ShowGenerator.class.getResource("/").getPath() + ftlPath);
			
			HashMap<String, Object> contextMap = new HashMap<String, Object>();
			contextMap.put("title", bean.title);
			contextMap.put("modular", bean.modular);
			contextMap.put("modelName", bean.modelName);
			contextMap.put("className", bean.className);
			contextMap.put("visitUrl", bean.path.visitUrl);
			contextMap.put("searchList", generatorList());
			contextMap.put("gridList", getGridList());
			contextMap.put("comboMap", bean.comboMaps);
			contextMap.put("height", getHeight(generatorList().size(), 3, 50));
			
			GeneratorHtml generator = new GeneratorHtml();
			generator.geneHtmlFile("show.ftl", ftlFile.getPath(), contextMap, getPathWithEnd(bean.path.jsp), bean.modelName +"List.html");
			
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return false;
	}

	/**
	 * 生成th标题
	 * 
	 * @return
	 */
	public List<Column> generatorList() {
		List<Column> list = bean.columns;
		List<Column> searchList = new ArrayList<Column>();
		for (Column col : list) {
			if (col.getSearch()) {
				searchList.add(col);
			}
		}
		
		return searchList;
	}

	/**
	 * 查看列表
	 * 
	 * @return
	 */
	public List<Column> getGridList() {
		List<Column> list = bean.columns;
		List<Column> viewList = new ArrayList<Column>();
		for (Column col : list) {
			if (col.getGrid()) {
				viewList.add(col);
			}
		}
		
		return viewList;
	}
}
